﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CinemaManagement.EF;
using CinemaManagement.Models;
using CinemaManagement.Utility;

namespace CinemaManagement.Controllers
{
    public class UserController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            var movies = GetMovies();
            ViewBag.movies =
            ViewBag.MovieNoFind = "没找到";
            ViewBag.TableFooter = "到底了";
            return View(movies);
        }

        [HttpPost]
        public ActionResult Index(string name, string type)
        {
            var movies = GetMovies(name, type);
            ViewBag.movies = movies;
            ViewBag.MovieNoFind = "没找到";
            ViewBag.TableFooter = "到底了";
            return View(movies);
        }

        public ActionResult Details(int movieId)
        {
            Movie movie = DbHelper.Instance.Db.Movie.FirstOrDefault(o => o.movieId == movieId);
            return View(movie);
        }
        public ActionResult BuyTicket(int movieId, int hall, DateTime showTime)
        {
            CurrentUser currentUser = null;
            var sessionUser = HttpContext.Session["CurrentUser"];
            if (sessionUser != null && sessionUser is CurrentUser)
            {
                currentUser = (CurrentUser)HttpContext.Session["CurrentUser"];
            }
            Orders order = new Orders()
            {
                userId = currentUser.Id,
                movieId = movieId,
                orderTime = DateTime.Now,
                orderHall = hall,
                releaseTime = showTime,
            };
            DbHelper.Instance.Db.Orders.Add(order);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Order()
        {

            var orderInfos = GetUserOrderInfos();

            return View(orderInfos);
        }
        public ActionResult Unsubscribe(int orderId)
        {
            UnsubscribeOrderById(orderId);
            //TODO:退订
            return RedirectToAction("Order");
        }
        #region private method
        private List<Movie> GetMovies()
        {
            return DbHelper.Instance.Db.Movie.ToList();
        }
        private List<Movie> GetMovies(string name, string type)
        {
            List<Movie> movies = DbHelper.Instance.Db.Movie.ToList();
            if (!string.IsNullOrEmpty(name))
            {
                movies = movies.Where(m => m.movieName.IndexOf(name) > -1).ToList();
            }
            if (!string.IsNullOrEmpty(type))
            {
                movies = movies.Where(m => m.movieType.Contains(type)).ToList();
            }
            return movies;
        }
        private IEnumerable<OrderInfo> GetUserOrderInfos()
        {
            CurrentUser currentUser = null;
            var sessionUser = HttpContext.Session["CurrentUser"];
            if (sessionUser != null && sessionUser is CurrentUser)
            {
                currentUser = (CurrentUser)HttpContext.Session["CurrentUser"];
            }
            List<OrderInfo> orderInfos = new List<OrderInfo>();
            if (currentUser == null)
            {
                return orderInfos;
            }
            string userName = DbHelper.Instance.Db.Customer.FirstOrDefault(o => o.userId == currentUser.Id)?.userName;
            var orders = DbHelper.Instance.Db.Orders.Where(o => o.userId == currentUser.Id).ToList();
            foreach (var order in orders)
            {
                string movieName = DbHelper.Instance.Db.Movie.FirstOrDefault(o => o.movieId == order.orderId)?.movieName;
                var temp = new OrderInfo()
                {
                    OrderId = order.orderId,
                    UserName = userName,
                    MovieName = movieName,
                    OrderTime = order.orderTime,
                    OrderHall = order.orderHall,
                    ReleaseTime = order.releaseTime
                };
                orderInfos.Add(temp);
            }
            return orderInfos;
        }
        private void UnsubscribeOrderById(int id)
        {
            DbHelper.Instance.Db.Orders.Remove(DbHelper.Instance.Db.Orders.FirstOrDefault(o => o.orderId == id));
        }
        #endregion
    }
}